package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.DisinfecResultDetailItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.AntMetCdItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.EquKindItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.MedCdItem;
import egovframework.npqs.pqi.mobile.jsoncontent.listitem.QuarRsltDtailItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;

public class DisinfectionDetailThread extends BasicThread {

	public interface DisinfectionDetailThreadCallback {
		public void OnDisinfectionDetailCompleted(boolean isNetworkConntected
				, DisinfecResultDetailItem quarantineItem, String message);
		public void OnDisinfectionDetailError(ErrorItem errorItem);
	}

	private String url;
	private DisinfectionDetailThreadCallback callback;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private String planSn, recNo;
	private boolean flag;

	public DisinfectionDetailThread(Context context, DisinfectionDetailThreadCallback callback
			, String planSn, String recNo, boolean flag, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.planSn = planSn;
		this.recNo = recNo;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.flag = flag;
	}

	@Override
	public void run() {
		Map<String, String> map = new HashMap<String, String>();
		String param = "";
		
		if(flag){
			map.put(Const.PLANSN, planSn);
			map.put(Const.RECNO, recNo);
			param = parameterReturn(Const.REQ017, (HashMap<String, String>) map);
		}else{
			map.put(Const.PLANSN, planSn);
			map.put(Const.RECNO, recNo);
			param = parameterReturn(Const.REQ051, (HashMap<String, String>) map);
		}
		
		Log.i("param", param);
		
		serviceBroker(param, context);
		
	}

	@Override
	public void settingObject() {
		
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnDisinfectionDetailCompleted(false, null, "통신 에러");
			return;
		}
		
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnDisinfectionDetailError(null);
				return;
			}
			int result = 0;
			result = json.getInt(Const.RESULT);
			/*
			int itemsResult = 0;
			itemsResult = json.getInt(Const.RESULT);
			*/
			Log.i("ITEMSRESULT", "ITEMSRESULT"+json.getString(DisinfecResultDetailItem.ITEMSRESULT));
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);
				callback.OnDisinfectionDetailError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arraydisinfection = json.getJSONArray(Const.DATA);
				JSONObject disinfection = arraydisinfection.getJSONObject(0);
				DisinfecResultDetailItem disinfectionItem = new DisinfecResultDetailItem();
				ArrayList<QuarRsltDtailItem> arrayItems = new ArrayList<QuarRsltDtailItem>();
				JSONArray items = json.getJSONArray(DisinfecResultDetailItem.ITEMS);
				for(int i = 0; i < items.length(); i++) {
					JSONObject itemObject = items.getJSONObject(i);
					QuarRsltDtailItem quarRsltDtailItem = new QuarRsltDtailItem();
					quarRsltDtailItem.setOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.OFFCD)));
					quarRsltDtailItem.setRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.RECNO)));
					quarRsltDtailItem.setRecSeq(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.RECSEQ)));
					quarRsltDtailItem.setDsnfTyName(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.DSNFTYNAME)));
					quarRsltDtailItem.setDsnfResOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.DSNFRESOFFCD)));
					quarRsltDtailItem.setDsnfResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.DSNFRESNO)));
					quarRsltDtailItem.setVenNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.VENNM)));
					quarRsltDtailItem.setRecSeq(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.RECSEQ)));
					quarRsltDtailItem.setPrdlstCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.PRDLSTCODE)));
					quarRsltDtailItem.setPrdlstName(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.PRDLSTNAME)));
					quarRsltDtailItem.setInsQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.INSQTY)));
					quarRsltDtailItem.setAntQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.ANTQTY)));
					quarRsltDtailItem.setQunCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.QUNCD)));
					quarRsltDtailItem.setQunNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.QUNNM)));
					quarRsltDtailItem.setDsnfCmmndNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.DSNFCMMNDNO)));
					quarRsltDtailItem.setDocScrtySe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.DOCSCRTYSE)));
					quarRsltDtailItem.setResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.RESNO)));
					quarRsltDtailItem.setResOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.RESOFFCD)));
					quarRsltDtailItem.setSanctnYn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, QuarRsltDtailItem.SANCTNYN)));
					arrayItems.add(quarRsltDtailItem);
				}
				disinfectionItem.setItems(arrayItems);
				
				ArrayList<AntMetCdItem> arrayAnt = new ArrayList<AntMetCdItem>();
				JSONArray antitems	 	= json.getJSONArray(DisinfecResultDetailItem.ANTMETCDLIST);
				for(int i = 0; i < antitems.length(); i++) {
					JSONObject itemObject = antitems.getJSONObject(i);
					AntMetCdItem antItem = new AntMetCdItem();
					antItem.setCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, AntMetCdItem.CODE)));
					antItem.setCodeNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, AntMetCdItem.CODENM)));
					arrayAnt.add(antItem);
				}
				disinfectionItem.setAntMetCdList(arrayAnt);

				ArrayList<EquKindItem> arrayEqu = new ArrayList<EquKindItem>();
				JSONArray equitems	 	= json.getJSONArray(DisinfecResultDetailItem.EQUKINDLIST);
				for(int i = 0; i < equitems.length(); i++) {
					JSONObject itemObject = equitems.getJSONObject(i);
					EquKindItem equItem = new EquKindItem();
					equItem.setCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, EquKindItem.CODE)));
					equItem.setCodeNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, EquKindItem.CODENM)));
					arrayEqu.add(equItem);
				}
				disinfectionItem.setEquKindList(arrayEqu);

				ArrayList<MedCdItem> arrayMed = new ArrayList<MedCdItem>();
				JSONArray meditems	 	= json.getJSONArray(DisinfecResultDetailItem.MEDCDLIST);
				for(int i = 0; i < meditems.length(); i++) {
					JSONObject itemObject = meditems.getJSONObject(i);
					MedCdItem medItem = new MedCdItem();
					medItem.setCode(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, MedCdItem.CODE)));
					medItem.setCodeNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(itemObject, MedCdItem.CODENM)));
					arrayMed.add(medItem);
				}
				disinfectionItem.setMedCdList(arrayMed);
				disinfectionItem.setItemsResult(json.getString(DisinfecResultDetailItem.ITEMSRESULT));
				disinfectionItem.setPlanSn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.PLANSN)));
				disinfectionItem.setEntrpsUnitySn(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ENTRPSUNITYSN)));
				disinfectionItem.setCprNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.CPRNM)));
				disinfectionItem.setEmpNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.EMPNM)));
				disinfectionItem.setTelNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.TELNO)));
				disinfectionItem.setDsnfInspctPlace(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.DSNFINSPCTPLACE)));
				disinfectionItem.setMdctDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MDCTDE)));
				disinfectionItem.setMdctHh(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MDCTHH)));
				disinfectionItem.setMdctMi(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MDCTMI)));
				disinfectionItem.setOpnDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPNDE)));
				disinfectionItem.setOpnHh(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPNHH)));
				disinfectionItem.setOpnMi(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPNMI)));
				disinfectionItem.setAntMin(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ANTMIN)));
				disinfectionItem.setAntMetCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ANTMETCD)));
				disinfectionItem.setMedCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MEDCD)));
				disinfectionItem.setMdctStackCnt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MDCTSTACKCNT)));
				disinfectionItem.setEquKind(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.EQUKIND)));
				disinfectionItem.setMedQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MEDQTY)));
				disinfectionItem.setAntMet(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ANTMET)));
				disinfectionItem.setFumQty(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.FUMQTY)));
				disinfectionItem.setRotDiv(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ROTDIV)));
				disinfectionItem.setRooTemp(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.ROOTEMP)));
				disinfectionItem.setMedNamCd1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MEDNAMCD1)));
				disinfectionItem.setMedNamCd2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MEDNAMCD2)));
				disinfectionItem.setMedNamNm1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MEDNAMNM1)));
				disinfectionItem.setMedNamNm2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.MEDNAMNM2)));
				disinfectionItem.setOpeNamCd1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPENAMCD1)));
				disinfectionItem.setOpeNamCd2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPENAMCD2)));
				disinfectionItem.setOpeNamNm1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPENAMNM1)));
				disinfectionItem.setOpeNamNm2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OPENAMNM2)));
				disinfectionItem.setRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.RECNO)));
				disinfectionItem.setOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.OFFCD)));
				disinfectionItem.setImgResult1(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.IMGRESULT1)));
				disinfectionItem.setImgResult2(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.IMGRESULT2)));
				disinfectionItem.setFileId(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(disinfection, DisinfecResultDetailItem.FILEID)));
				callback.OnDisinfectionDetailCompleted(true, disinfectionItem, mResult);
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}
}